import { fileURLToPath, URL } from 'node:url'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import path from 'path'
import basicSsl from '@vitejs/plugin-basic-ssl'



const targetUrl = "https://scaptest.scrcu.com.cn/sit5"
// const targetUrl = "https://172.16.1.201"


// https://vite.dev/config/
export default defineConfig({
  base: './',
    plugins: [
      vue(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver(
          {
            importStyle: "sass",
          }
        )],
      }),
      basicSsl(),
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)), // 配置 @ 指向 src 目录
        // '@': path.resolve(__dirname, './src'), // 配置 @ 指向 src 目录
      },
    },
    server: {
      host: "0.0.0.0",
      port: 8082, // 端口号
      // https: false, // https:{type:Boolean}
      open: true, // 自动启动浏览器
      proxy: {
        '/manage-api': {
          target: targetUrl,
          changeOrigin: true,
          rewrite: path => path.replace(RegExp(`^\/manage-api`), '/manage-api'),
          // secure: true,// 是否https接口
          bypass(req: any, res: any, options: any) {
            const proxyUrl = new URL(options.rewrite(req.url) || '',(options.target)as string)?.href || '';
            console.log('代理请求api:', proxyUrl);
            req.headers['x-req-proxyUrl'] = proxyUrl
            res.setHeader('x-res-proxyUrl',proxyUrl)
          },
        },
        '/manage-upload': {
          target: targetUrl,
          changeOrigin: true,
          rewrite: path => path.replace(RegExp(`^\/manage-upload`), '/manage-upload'),
          // secure: true,// 是否https接口
          bypass(req: any, res: any, options: any) {
            const proxyUrl = new URL(options.rewrite(req.url) || '',(options.target)as string)?.href || '';
            console.log('代理请求upload:', proxyUrl);
            req.headers['x-req-proxyUrl'] = proxyUrl
            res.setHeader('x-res-proxyUrl',proxyUrl)
          },
        },
        '/oss-private': {
          target: targetUrl,
          changeOrigin: true,
          rewrite: path => path,
          // secure: true,// 是否https接口
          bypass(req: any, res: any, options: any) {
            const proxyUrl = new URL(options.rewrite(req.url) || '',(options.target)as string)?.href || '';
            console.log('代理请求oss:', proxyUrl);
            req.headers['x-req-proxyUrl'] = proxyUrl
            res.setHeader('x-res-proxyUrl',proxyUrl)
          },
        },
      }
    },
})

